package com.xcf.controller.business.wharfmanage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.service.business.wharfmanage.DomesticTradeFatchBillService;
import com.xcf.util.DateUtil;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import cn.com.xcf.dao.model.FactBookingNote;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/domestic")
public class DomesticTradeFatchBillController extends BaseController  implements ExportDataHandler{
 @Resource
 private DomesticTradeFatchBillService domsticbill;
 String menuUrl="domestic/TradeFatchBill";//菜单地址(权限用) 
 @RequestMapping(value="/TradeFatchBill")
 public ModelAndView domestic(){
	 ModelAndView mv = new ModelAndView();
	 mv.setViewName("/business/wharfmanage/domestictradeFatchBill/domestictradeFatchBill");
	return mv;	 
 }
 
 @RequestMapping(value="/list")
 @ResponseBody
	public ResponseResult list(int page,int limit) throws Exception{
		PageData pd = new PageData();
		pd=this.getPageData();
		PageHelper.startPage(page, limit);
		List<PageData>list=domsticbill.sear(pd);//查询托单表
		List<PageData>list1= new ArrayList<>();
		for (PageData data : list) {
			if(data.get("CREATE_TIME")!=null&&data.get("CREATE_TIME")!=""){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String CREATE_TIME = sdf.format(data.get("CREATE_TIME"));
				data.put("CREATE_TIME",CREATE_TIME );//预计放箱时间
			}else{
				data.put("CREATE_TIME", data.get("CREATE_TIME"));//接单时间
			}
			if(data.get("IS_BOOKING_CARGO")!=null&&data.get("IS_BOOKING_CARGO").toString().equals("1")){//判断订舱
			   if(data.get("IS_PUT_BOX")!=null&&data.get("IS_PUT_BOX").toString().equals("1")){//判断放箱
				if(data.get("IS_COST_COMPLETED")!=null&&data.get("IS_COST_COMPLETED").toString().equals("1")){//判断费用完成
					if(data.get("IS_CHECKED_ACCOUNT")!=null&&data.get("IS_CHECKED_ACCOUNT").toString().equals("1")){//判断对账
						if(data.get("IS_INVOICE_TICKET")!=null&&data.get("IS_INVOICE_TICKET").toString().equals("1")){//判断开票
							if(data.get("IS_RECEIPT")!=null&&data.get("IS_RECEIPT").toString().equals("1")){//判断收款
								String BILL_STATUS=null;
								BILL_STATUS="收款";
								data.put("BILL_STATUS", BILL_STATUS);
							}else{
								String BILL_STATUS=null;
								BILL_STATUS="开票";
								data.put("BILL_STATUS", BILL_STATUS);
							}
						}else{
							String BILL_STATUS=null;
							BILL_STATUS="对账";
							data.put("BILL_STATUS", BILL_STATUS);
						}	
						}else{
							String BILL_STATUS=null;
							BILL_STATUS="费用完成";
							data.put("BILL_STATUS", BILL_STATUS);
						}
				}else{
					String BILL_STATUS=null;
					BILL_STATUS="放箱";
					data.put("BILL_STATUS", BILL_STATUS);
				}	
				}else{
					String BILL_STATUS=null;
					BILL_STATUS="订舱";
					data.put("BILL_STATUS", BILL_STATUS);
				}
				
			}else{
				String BILL_STATUS=null;
				BILL_STATUS="输机";
				data.put("BILL_STATUS", BILL_STATUS);
			}
//			if(data.getString("SUB_SUPPLIER")!=null&&data.getString("SUB_SUPPLIER")!=""){
//				if(data.getString("SUB_SUPPLIER").contains("新长丰")||data.getString("SUB_SUPPLIER").contains("锦海捷亚")){
//					String subSupplier=null;
//					subSupplier="内订舱";
//					data.put("SUB_SUPPLIER", subSupplier);
//				}else{
//					String subSupplier=null;
//					subSupplier="外订舱";
//					data.put("SUB_SUPPLIER", subSupplier);
//				}
//			}else{
//				data.put("SUB_SUPPLIER", data.getString("SUB_SUPPLIER"));
//			}
			
			data.put("one", "1");//换单份数
			list1.add(data);
		}
		return ResponseResult.success(new PageInfo(list));
//		JSONArray arry = new JSONArray();
//		for (PageData singlecarrier : list) {
//			JSONObject ob = new JSONObject();
//			ob.put("ID", singlecarrier.get("ID"));//ID
//			ob.put("BILL_CODE", singlecarrier.getString("BILL_CODE"));//托单编号
//			if(singlecarrier.get("BILL_STATUS")==null||singlecarrier.get("BILL_STATUS").equals("0")){
//				String billstatus="输机";
//				ob.put("BILL_STATUS",billstatus );//托单状态
//			} else if(singlecarrier.get("BILL_STATUS").equals("1")){
//				String billstatus="放箱";
//				ob.put("BILL_STATUS",billstatus );//托单状态
//			}else if(singlecarrier.get("BILL_STATUS").equals("2")){
//				String billstatus="订舱";
//				ob.put("BILL_STATUS",billstatus );//托单状态
//			}
//			ob.put("LADING_BILL_CODE", singlecarrier.getString("LADING_BILL_CODE"));//提单编号
//			ob.put("one", "1");//换单份数
//			ob.put("BUSINESS_TYPE", singlecarrier.getString("BUSINESS_TYPE"));//贸易类型,业务类型
//			ob.put("", singlecarrier.getString(""));//客服服务
//			ob.put("FORWARDER_CUSTOMER_SERVICE", singlecarrier.getString("FORWARDER_CUSTOMER_SERVICE"));//货代客服
//			ob.put("SHIPPER", singlecarrier.getString("SHIPPER"));//委托单位
//			ob.put("BOAT_NAME", singlecarrier.getString("BOAT_NAME"));//江船船名
//			ob.put("BOAT_VOYAGE_NAME", singlecarrier.getString("BOAT_VOYAGE_NAME"));//江船航次
//			if(singlecarrier.get("CREATE_TIME")!=null&&singlecarrier.get("CREATE_TIME")!=""){
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//				String CREATE_TIME = sdf.format(singlecarrier.get("CREATE_TIME"));
//				ob.put("CREATE_TIME",CREATE_TIME );//预计放箱时间
//			}else{
//				ob.put("CREATE_TIME", singlecarrier.get("CREATE_TIME"));//接单时间
//			}
//			
//			ob.put("RESERVE_MATCH_BOX_MASTER", singlecarrier.getString("RESERVE_MATCH_BOX_MASTER"));//预配箱主
//			ob.put("SHIPPER_NUM", singlecarrier.getString("SHIPPER_NUM"));//托运人自编号
//			ob.put("SINGLE_TYPE", singlecarrier.getString("SINGLE_TYPE"));//托单类型
//			ob.put("BILL_FLOW", singlecarrier.getString("BILL_FLOW"));//托单流向
//			//ob.put("", singlecarrier.getString(""));//订舱性质
//			ob.put("RIVER_PORT", singlecarrier.getString("RIVER_PORT"));//江船起运港
//			ob.put("RIVER_LOAD", singlecarrier.getString("RIVER_LOAD"));//江船装区
//			ob.put("RIVER_UNLOAD", singlecarrier.getString("RIVER_UNLOAD"));//江船卸货港
//			ob.put("RIVER_DELIVERY", singlecarrier.getString("RIVER_DELIVERY"));//江船交货地
//			ob.put("CHINESE_NAME", singlecarrier.getString("CHINESE_NAME"));//货物名称
//			ob.put("GOODS_TYPE", singlecarrier.getString("GOODS_TYPE"));//货物种类
//			ob.put("BOX_TYPE_CONFIRM", singlecarrier.getString("BOX_TYPE_CONFIRM"));//箱型确定
//			ob.put("TWENTY_GP", singlecarrier.get("TWENTY_GP"));//20
//			ob.put("FORTY_GP", singlecarrier.get("FORTY_GP"));//40
//			ob.put("FORTY_HP", singlecarrier.get("FORTY_HP"));//40H
//			ob.put("FORTYFive", singlecarrier.get("FORTY_FIVE"));//45
//			ob.put("SPECIAL_CONTAINER", singlecarrier.get("SPECIAL_CONTAINER"));//特种箱
//			ob.put("TEU", singlecarrier.get("TEU"));//TEU
//			ob.put("GOODS_NUMBER", singlecarrier.getString("GOODS_NUMBER"));//件数
//			ob.put("getGROSS_WEIGHT", singlecarrier.get("getGROSS_WEIGHT"));//重量
//			ob.put("VOLUME", singlecarrier.getString("VOLUME"));//体积
//            ob.put("CONTAINER_NO", singlecarrier.getString("CONTAINER_NO"));//箱号
//			arry.add(ob);
//			}  
//		response.setContentType("text/html; charset=utf-8");         
//     response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows"+"\""+":"+arry+"}");
	}
@Override
public List getData(String type, Map param, Page page) {
	try {
	if(MapUtils.isEmpty(param) || param.get("BILL_CODE") == null || StringUtils.isBlank(param.get("BILL_CODE").toString())){
		return Lists.newArrayList();
	}
	if("domesticTradeFatchBill".equalsIgnoreCase(type)){

	}
	if("Billading".equalsIgnoreCase(type)){

	}
	PageData pd = new PageData();
	pd.put("BILL_CODE", param.get("BILL_CODE").toString());
	pd.put("ID", param.get("ID"));
	PageData list = domsticbill.queryByBillCode(pd);
	return Lists.newArrayList(list);
	} catch (Exception e) {
	
		e.printStackTrace();
	}
	return null;
	
}
@Override
public String getService() {
	return  getClass().getSimpleName();
}
@Override
public Map<String, String> setNames() {
	Map m = new HashMap(3);
	m.put("domesticTradeFatchBill","domesticTradeFatchBill.xls");
	m.put("domesticTradeFatchBillName","内贸提单("+ DateUtil.getSdfTimes()+").xls");
	m.put("domesticTradeFatchBillModule",getService());
	
	m.put("Billading","Billading.xls");
	m.put("BilladingName","提货单("+ DateUtil.getSdfTimes()+").xls");
	m.put("BilladingModule",getService());
	return m;
 
}
 

}
